<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../vue.js"></script>
    <style>
        .done {
            color: gray;
            text-decoration: line-through;
        }
    </style>
</head>
<body>
    <div id="app">
        <h1>任务列表</h1>
        <p>任务总数：{{totalList.length}},还有{{todoList.length}}项未完成</p>
        <input type="text" v-model="thing" @keyup.enter="add" placeholder="请输入待办事项...">
        <p>待办事项</p>
        <ul>
            <li v-for="(todo,index) in todoList" :key="index">
                <span>{{todo.name}}</span>
                <button @click="del(todo.id)">del</button>
            </li>
        </ul>
        <p>完成事项</p>
        <ul class="done">
            <li v-for="(todo,index) in doneList" :key="index+'done'">
                <span>{{todo.name}}</span>
            </li>
        </ul>
    </div>
    <script>
        let vm = new Vue({
            el: '#app',
            data: {
                // 待完成
                totalList: [
                    // done是否完成
                    {name: 'eat', done: false, id: 1},
                    {name: 'sleep', done: false, id: 2},
                    {name: 'coding', done: false, id: 3},
                ],
                thing: '',
            },
            computed: {
                todoList() {
                    // fliter 筛选数组，返回一个符合条件的项组成的新数组
                    let arr = this.totalList.filter(item => {
                        return !item.done
                    })
                    return arr
                },
                doneList() {
                    // fliter 筛选数组，返回一个符合条件的项组成的新数组
                    let arr = this.totalList.filter(item => {
                        return item.done
                    })
                    return arr
                }
            },
            methods: {
                add() {
                    if(this.thing.trim()) { // 输入框为空时 不能添加,空字符是假，有内容才是真，才会执行
                        // 添加到代办事项的列表中
                        this.totalList.push(
                            {
                                name: this.thing,
                                // 默认未完成
                                done: false,
                                id: this.todoList.length+1
                            }
                        );
                        // 置空输入框
                        this.thing = '';
                    }
                },
                // 根据下标删除
                del(id) {
                    this.totalList.forEach(item => {
                        if(item.id == id) {
                            console.log(item);
                            item.done = true;
                        }
                    });
                }
            }
        })
    </script>
</body>
</html>